﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Add_ShipTo : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public frm_Add_ShipTo()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
        }
                
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;
            string shipto_code = null;
            string shipto_name = null;
            string shipto_pic = null;
            string address = null;
            string telephone = null;
            string fax = null;
            int export = 0;
            string note = null;

            if (string.IsNullOrEmpty(this.shiptoCode.Text))
            {
                error = "Please enter ShipTo Code";
            }
            else
            {
                shipto_code = this.shiptoCode.Text;
            }
            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.shiptoName.Text))
                {
                    error = "Please enter ShipTo Name";
                }
                else
                {
                    shipto_name = this.shiptoName.Text;
                }
            }
            if (string.IsNullOrEmpty(error))
            {
                shipto_pic = this.shiptoPIC.Text;
                address = this.addressText.Text;                
                fax = this.faxNumber.Text;
                telephone = this.phoneNumber.Text;
                if (this.exportCheck.Checked)
                {
                    export = 1;
                }
                else
                {
                    export = 0;
                }
                note = this.noteText.Text;
                            
                try
                {
                    myConnection.Open();
                    string sql = "INSERT INTO [222_ShipTo] ([ShipToCode], [ShipToName], [SPIC], [Address],"
                        + " [Telephone], [Fax], [ImOrEx], [Note])"
                        + " VALUES ('" + shipto_code + "', N'" + shipto_name + "', '" + shipto_pic + "', N'" + address + "',"
                        + " '" + telephone + "', '" + fax + "', " + export + ", '" + note + "')";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);                    
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("ShipTo added success.");
                    clearForm();
                }
                catch (Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show(ex.Message);
                    //MessageBox.Show("Cannot add ShipTo. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        private void clearForm()
        {
            foreach (Control c in this.Controls)
            {
                if (c is DevExpress.XtraEditors.TextEdit)
                {
                    ((DevExpress.XtraEditors.TextEdit)c).Text = null;
                }

                if (c is DevExpress.XtraEditors.ComboBoxEdit)
                {
                    ((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex = -1;
                }

                if (c is System.Windows.Forms.MaskedTextBox)
                {
                    ((System.Windows.Forms.MaskedTextBox)c).Text = null;
                }
            }
        }
    }
}
